package com.youtubetv.controllers

import youtube.tv.VideoCategory;

class VideoCategoryController {
    def authenticationService
    def videoCategoryService
    def youtubeService
    def videoCategories
    def currentVideoCategory
    int startIndex

    // page variables
    def videos

    def index = {
        videoCategories = videoCategoryService.categories

        parseCommonParams()
        videos = youtubeService.getVideos(currentVideoCategory, startIndex, params.videoId)
    }

    def search = {
        parseCommonParams()
        videos = youtubeService.getVideos(currentVideoCategory, startIndex)
        render(contentType: "text/json") {
            startInd = startIndex
            vids = videos*.id
        }
    }

    def framesLine = {
        videoCategories = videoCategoryService.categories
        parseCommonParams()
        videos = youtubeService.getVideos(currentVideoCategory, startIndex)
        render template: "framesLine"
    }

    private void parseCommonParams() {
        if (params.feed) {
            currentVideoCategory = new VideoCategory(feed: params.feed, time: params.time)
        }
        else {
            currentVideoCategory = videoCategories[0]
        }
        startIndex = params.startIndex ? Integer.valueOf(params.startIndex) : 0
    }
}
